package com.SyDeck.sdPerkSystem;

import com.SyDeck.sdPerkSystem.PerksCmds.Teleport;
import com.SyDeck.sdPerkSystem.prop.sdPerksProperties;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author Deck & Sammy
 */
public class sdP extends JavaPlugin {

    private static final String PerksDIR = "plugins/RPGmod/Rank/";
    private static final File accounts = new File(PerksDIR + "PlayersRank.txt");
    private static final Logger log = Logger.getLogger("Minecraft");
    public PermissionHandler Permissions;
    private boolean UsePermissions;

    public static void main(String[] args) {
        // TODO code application logic here
    }

    public void onDisable() {
        System.out.println("disabled, goodbye wold");
    }

    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        PluginManager pm = getServer().getPluginManager();
        new File(PerksDIR).mkdirs();
        if (!accounts.exists()) {
            try {
                accounts.createNewFile();
            } catch (IOException ex) {
                log.log(Level.INFO, ex.getMessage(), pdfFile.getName());
            }
        }
        sdPerksProperties.loadMiningProps();
        setupPermissions();
        getCommand("bringto").setExecutor(new Teleport(this));
        getCommand("accept").setExecutor(new Teleport(this));
        //  getCommand("perks").setExecutor(new sdRankCmds(this));
        System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
    }

    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions is enable for sdBountyHunter.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }

    public boolean casUseRankList(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "rank.list");
        }
        return p.isOp();
    }
}
